package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.server.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: RoleController
 * Package: com.atguigu.ssyx.acl.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-09-19
 * Time: 19:48
 */

@Api(tags = "角色接口")
@RestController
@RequestMapping("/admin/acl/role")

public class RoleController {

    @Autowired
    private RoleService roleService;


    //1.角色的条件分页查询
    // /admin/acl/user/1/5?username=
    @ApiOperation("角色分页查询")
    @GetMapping("/{page}/{limit}")
    public Result pageList(@PathVariable Long page,
                           @PathVariable Long limit,
                           RoleQueryVo roleQueryVo) {
        //条件查询类 专门封装到了Vo里面
        //创建分页对象
        Page<Role> rolePage = new Page<>(page, limit);

        //调用Service查询 返回分页对象 具体查询逻辑 写在impl实现类中
        IPage<Role> iPage = roleService.selectRolePage(rolePage, roleQueryVo);
        //返回查询结果
        return Result.ok(iPage);
    }

    //2.根据id查询角色
    @ApiOperation("根据ID查询角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        Role byId = roleService.getById(id);
        return Result.ok(byId);
    }

    //3.添加角色
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody Role role) {
        boolean save = roleService.save(role);
        if (save) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    //http://localhost:8201/admin/acl/role/update
    //4.修改角色
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        //根据id来判断修改的角色
        roleService.updateById(role);
        return Result.ok(null);
    }

    //5.根据id删除角色
    @ApiOperation("根据ID删除角色")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable Long id) {
        boolean b = roleService.removeById(id);
        return Result.ok(b);
    }

    //6.批量删除角色
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        //[1,,2,3]JSON格式的数据 对应Java集合
        //批量删除 那么一次到得到多个id值[1,,2,3]JSON格式的数组格式
        roleService.removeByIds(idList);

        return Result.ok(null);

    }

}
